package com.hemerasolutions.v2trainer.component.search;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hemerasolutions.v2trainer.component.BaseComponent;
import com.hemerasolutions.v2trainer.component.interfaces.detail.TrainingDetailsCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.list.StepListCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.search.TrainingSearchCompInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.TrainingSearchDelegateInterface;
import com.hemerasolutions.v2trainer.model.library.Library;
import com.hemerasolutions.v2trainer.model.modality.Modality;
import com.hemerasolutions.v2trainer.model.training.Training;
import com.hemerasolutions.v2trainer.model.training.TrainingStep;
import com.hemerasolutions.v2trainer.model.training.TrainingType;
import com.hemerasolutions.v2trainer.service.interfaces.TrainingServiceInterface;

@Component("trainingSearchComp")
@Scope("prototype")
public class TrainingSearchComp extends BaseComponent implements TrainingSearchCompInterface{

	private static final long serialVersionUID = 1L;

	private Training training;
	
	private Training selTraining;

	private TrainingSearchDelegateInterface trainingSearchDelegate;
	
	private List<TrainingType> findTrainingByTrainingTypes;

	private List<Modality> findTrainingByModalitys;
	
	private String trainingFindByName;

	private String trainingFindByDescription;

	private String trainingFindByObs;

	private List<TrainingType> fullTrainingTypes;
	
	private List<Modality> fullModalitys;
	
	private Date trainingBegCreatedAt;
	
	private Date trainingEndCreatedAt;
	
	private Date trainingBegChangedAt;
	
	private Date trainingEndChangedAt;
	
	private boolean trainingFindByStatus;
	
	private int importLevel;
	
	private List<Training> foundTrainings;
	
	@Autowired
	private TrainingDetailsCompInterface trainingDetails;
	
	@Autowired
	private TrainingServiceInterface trainingService;

	@Autowired
	private StepListCompInterface stepListHandler;
	
	
	@PostConstruct
	public void init(){
		super.init();
	}
	
	
	@Override
	public void refresh(){
		this.fullTrainingTypes = trainingService.listTrainingTypes();
		this.trainingFindByStatus = true;
		this.fullModalitys = new ArrayList<Modality>();
		
	}


	
	@Override
	public void addTraining(){
		this.trainingSearchDelegate.onTrainingAdd(this.selTraining);
	}
	
	
	
	@Override
	public void toViewTraining(){
		this.trainingDetails.setTraining(this.training);
		this.trainingDetails.refresh();
	}
	
	
	@Override
	public void changeFullTrainingTypesAll(org.primefaces.event.ToggleSelectEvent ev){
		
		if(ev.isSelected()){
			this.findTrainingByTrainingTypes = this.fullTrainingTypes;
		}else{
			this.findTrainingByTrainingTypes = new ArrayList<TrainingType>();
		}
		
		this.changeFullTrainingTypes();
	}	
	

	
	@Override
	public void changeFullModalitysAll(org.primefaces.event.ToggleSelectEvent ev){
		
		if(ev.isSelected()){
			this.findTrainingByModalitys = this.fullModalitys;
		}else{
			this.findTrainingByModalitys = new ArrayList<Modality>();
		}
		
		this.changeFullTrainingTypes();
	}	
	
	
	
	@Override
	public void changeFullTrainingTypes(){
		this.fullModalitys = new ArrayList<Modality>();
		for(Library lib:this.getActiveUserAllowedLibrarys()){
			for(Modality mod:lib.getVersionableModalitys()){
				if(this.findTrainingByTrainingTypes.contains(mod.getTrainingType())){
					this.fullModalitys.add(mod);
				}
			}
		}
	}
	
	
	@Override
	public void searchTrainings(){

		Map<String,Object> fields = new HashMap<String, Object>();
    	
		if( this.trainingBegCreatedAt != null && this.trainingEndCreatedAt != null && 
				this.trainingBegCreatedAt.compareTo(this.trainingEndCreatedAt) > 0){
				this.showErrorMessage(	this.localizedText("CreationDate"),
										this.localizedText("TheInitialDateNeedToBeGreaterThanFinal"));
				return;
			}
			

		if( this.trainingBegChangedAt != null && this.trainingEndChangedAt != null && 
			this.trainingBegChangedAt.compareTo(this.trainingEndChangedAt) > 0){
			this.showErrorMessage(	this.localizedText("ChangeDate"),
									this.localizedText("TheInitialDateNeedToBeGreaterThanFinal"));
			return;			
		}
    	

    	if (!this.trainingFindByName.equals("")) {
            fields.put("name", this.trainingFindByName);
        }

    	if (!this.trainingFindByDescription.equals("")) {
            fields.put("description", this.trainingFindByDescription);
        }
    	
    	if (!this.trainingFindByObs.equals("")){
    		fields.put("obs",this.trainingFindByObs);
    	}

    	
    	if(this.findTrainingByTrainingTypes != null && !this.findTrainingByTrainingTypes.isEmpty() ){
    		fields.put("trainingTypes",this.findTrainingByTrainingTypes);
    	}
    	
    	if(this.findTrainingByModalitys != null && !this.findTrainingByModalitys.isEmpty() ){
    		fields.put("modalitys",this.findTrainingByModalitys);
    	}
    	
    	fields.put("trainingSystem",false);

    	fields.put("status", this.trainingFindByStatus);
    	
	    this.foundTrainings = this.trainingService.searchTrainings(fields);

		if((this.foundTrainings).isEmpty()){
			this.showInfoMessage(	this.localizedText("SearchCompleted"),
									this.localizedText("NoEntrysFounded"));
		}else{
			this.showInfoMessage(	this.localizedText("SearchCompleted"),
									this.foundTrainings.size() + " " + 
									this.localizedText("entrysFounded"));
		}
	    
		this.trainingSearchDelegate.onSearchComplete(this.foundTrainings);
	
	}

	
	
		
	@Override
	public void toConfigTraining(){
		
		for(TrainingStep tss: training.getTrainingSteps() ){
			if(tss.getLevel() == this.importLevel){

				this.stepListHandler.setStepLevel(tss);
				this.stepListHandler.refresh();
				
				break;
			}
		}		
	}
	

	
	
	@Override
	public void transferSteps(){
		for(TrainingStep tss:this.training.getTrainingSteps()){
			if(tss.getLevel() == this.importLevel){
				this.trainingSearchDelegate.onTrainingImportSteps(tss.getRootStep().getSteps());
			}
		}
	}

	
	@Override
	public Training getTraining() {
		return training;
	}

	
	@Override
	public void setTraining(Training training) {
		this.training = training;
	}

	
	@Override
	public Training getSelTraining() {
		return selTraining;
	}

	
	@Override
	public void setSelTraining(Training selTraining) {
		this.selTraining = selTraining;
	}

	
	@Override
	public TrainingSearchDelegateInterface getTrainingSearchDelegate() {
		return trainingSearchDelegate;
	}

	
	@Override
	public void setTrainingSearchDelegate(
			TrainingSearchDelegateInterface tsSearchDelegate) {
		this.trainingSearchDelegate = tsSearchDelegate;
	}

	
	@Override
	public List<TrainingType> getFindTrainingByTrainingTypes() {
		return findTrainingByTrainingTypes;
	}

	
	@Override
	public void setFindTrainingByTrainingTypes(
			List<TrainingType> findTrainingByTrainingTypes) {
		this.findTrainingByTrainingTypes = findTrainingByTrainingTypes;
	}

	
	@Override
	public List<Modality> getFindTrainingByModalitys() {
		return findTrainingByModalitys;
	}

	
	@Override
	public void setFindTrainingByModalitys(
			List<Modality> findTrainingByModalitys) {
		this.findTrainingByModalitys = findTrainingByModalitys;
	}

	
	@Override
	public String getTrainingFindByName() {
		return trainingFindByName;
	}

	
	@Override
	public void setTrainingFindByName(String trainingFindByName) {
		this.trainingFindByName = trainingFindByName;
	}

	
	@Override
	public String getTrainingFindByDescription() {
		return trainingFindByDescription;
	}

	
	@Override
	public void setTrainingFindByDescription(
			String trainingFindByDescription) {
		this.trainingFindByDescription = trainingFindByDescription;
	}

	
	@Override
	public String getTrainingFindByObs() {
		return trainingFindByObs;
	}

	
	@Override
	public void setTrainingFindByObs(String trainingFindByObs) {
		this.trainingFindByObs = trainingFindByObs;
	}

	
	@Override
	public List<TrainingType> getFullTrainingTypes() {
		return fullTrainingTypes;
	}

	
	@Override
	public void setFullTrainingTypes(List<TrainingType> fullTrainingTypes) {
		this.fullTrainingTypes = fullTrainingTypes;
	}

	
	@Override
	public List<Modality> getFullModalitys() {
		return fullModalitys;
	}

	
	@Override
	public void setFullModalitys(List<Modality> fullModalitys) {
		this.fullModalitys = fullModalitys;
	}

	
	@Override
	public Date getTrainingBegCreatedAt() {
		return trainingBegCreatedAt;
	}

	
	@Override
	public void setTrainingBegCreatedAt(Date trainingBegCreatedAt) {
		this.trainingBegCreatedAt = trainingBegCreatedAt;
	}

	
	@Override
	public Date getTrainingEndCreatedAt() {
		return trainingEndCreatedAt;
	}

	
	@Override
	public void setTrainingEndCreatedAt(Date trainingEndCreatedAt) {
		this.trainingEndCreatedAt = trainingEndCreatedAt;
	}

	
	@Override
	public Date getTrainingBegChangedAt() {
		return trainingBegChangedAt;
	}

	
	@Override
	public void setTrainingBegChangedAt(Date trainingBegChangedAt) {
		this.trainingBegChangedAt = trainingBegChangedAt;
	}

	
	@Override
	public Date getTrainingEndChangedAt() {
		return trainingEndChangedAt;
	}

	
	@Override
	public void setTrainingEndChangedAt(Date trainingEndChangedAt) {
		this.trainingEndChangedAt = trainingEndChangedAt;
	}

	
	@Override
	public boolean isTrainingFindByStatus() {
		return trainingFindByStatus;
	}

	
	@Override
	public void setTrainingFindByStatus(boolean trainingFindByStatus) {
		this.trainingFindByStatus = trainingFindByStatus;
	}

	
	@Override
	public List<Training> getFoundTrainings() {
		return foundTrainings;
	}

	
	@Override
	public void setFoundTrainings(List<Training> foundTrainings) {
		this.foundTrainings = foundTrainings;
	}

	
	@Override
	public TrainingDetailsCompInterface getTrainingDetails() {
		return trainingDetails;
	}

	
	@Override
	public void setTrainingDetails(TrainingDetailsCompInterface tsDetails) {
		this.trainingDetails = tsDetails;
	}

	
	
	@Override
	public int getImportLevel() {
		return importLevel;
	}

	
	@Override
	public void setImportLevel(int importLevel) {
		this.importLevel = importLevel;
	}

	
	@Override
	public StepListCompInterface getStepListHandler() {
		return stepListHandler;
	}

	
	@Override
	public void setStepListHandler(StepListCompInterface stepListHandler) {
		this.stepListHandler = stepListHandler;
	}



}
